package com.example.word.ui.activity;

import android.app.Activity;
import android.content.ContentValues;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.dict.R;
import com.example.word.bean.Dict;
import com.example.word.bean.Word;
import com.example.word.util.StringUtils;
import com.example.word.widget.ActionBar;

import org.litepal.crud.DataSupport;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 添加页面
 */
public class AddActivity extends AppCompatActivity {
    private ActionBar mTitleBar;//标题栏
    private Activity myActivity;
    private EditText etTitle;//标题
    private EditText etImg;//图片
    private Spinner spType;//类型
    private EditText etContent;//内容
    private EditText etWordNumber;//数量
    private Button btnSave;//保存
    private ImageView ivImg;//图片
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private Dict currentDict;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myActivity = this;
        setContentView(R.layout.activity_add);
        etTitle = findViewById(R.id.title);
        spType = findViewById(R.id.dictType);
        etImg = findViewById(R.id.img);
        etContent = findViewById(R.id.content);
        etWordNumber = findViewById(R.id.wordNumber);
        ivImg = findViewById(R.id.iv_img);
        btnSave = findViewById(R.id.btn_save);
        mTitleBar = findViewById(R.id.myActionBar);
        //侧滑菜单
        mTitleBar.setData(myActivity, "编辑信息", R.drawable.ic_back, 0, 0, getResources().getColor(R.color.colorPrimary), new ActionBar.ActionBarClickListener() {
            @Override
            public void onLeftClick() {
                finish();
            }

            @Override
            public void onRightClick() {
            }
        });
        // 设置主题
        if (!com.example.word.ui.activity.MainActivity.colorChange) {
            mTitleBar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            btnSave.setBackground(getResources().getDrawable(R.drawable.btn_border_fill));
        } else {
            mTitleBar.setBackgroundColor(getResources().getColor(R.color.myColor));
            btnSave.setBackground(getResources().getDrawable(R.drawable.btn_border_fill_my_color));
        }
        initView();
    }

    private void initView() {
        //Activity之间通过Intent传递值
        currentDict = (Dict) getIntent().getSerializableExtra("dict");
        if (currentDict != null) {
            etTitle.setText(currentDict.getTitle());
            spType.setSelection(currentDict.getTypeId());
            etImg.setText(currentDict.getImg());
            etContent.setText(currentDict.getContent());
            etWordNumber.setText(String.valueOf(currentDict.getWordNumber()));
            spType.setSelection(currentDict.getTypeId(), true);
            Glide.with(myActivity)
                    .asBitmap()
                    .load(currentDict.getImg())
                    .error(R.drawable.ic_error)
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .into(ivImg);
        }
        ivImg.setVisibility(currentDict == null ? View.GONE : View.VISIBLE);
    }

    public void save(View view) {
        String title = etTitle.getText().toString();
        String img = etImg.getText().toString();
        String content = etContent.getText().toString();
        Integer typeId = spType.getSelectedItemPosition();
        if ("".equals(title)) {//如果输入为空
            Toast.makeText(myActivity, "标题不能为空", Toast.LENGTH_LONG).show();
            return;
        }
//        if ("".equals(issuer)) {
//            Toast.makeText(myActivity, "价格不能为空", Toast.LENGTH_LONG).show();
//            return;
//        }
        if ("".equals(img)) {
            Toast.makeText(myActivity, "图片地址不能为空", Toast.LENGTH_LONG).show();
            return;
        }
        if ("".equals(content)) {
            Toast.makeText(myActivity, "描述不能为空", Toast.LENGTH_LONG).show();
            return;
        }
//        if ("0".equals(wordNumber)) {
//                Toast.makeText(myActivity, "数量不能为0", Toast.LENGTH_LONG).show();
//                return;
//        }

        Dict dict = null;
        if (currentDict != null && !title.equals(currentDict.getTitle())) {
            //通过标题查找fruit里是否存在这个标题
            dict = DataSupport.where("title = ?", title).findFirst(Dict.class);
        }
        if (dict == null) {
            if (currentDict != null) {
                dict = DataSupport.where("title = ?", currentDict.getTitle()).findFirst(Dict.class);
                dict.setTypeId(typeId);
                dict.setTitle(title);
                dict.setImg(img);
                dict.setContent(content);
                ContentValues values = new ContentValues();
                values.put("dict", dict.getTitle());

                DataSupport.updateAll(Word.class, values, "dict = ?", currentDict.getTitle());
            } else {
                dict = new Dict(typeId, title, img,"downloaded", content, 0, sf.format(new Date()));
            }
            dict.save();
            setResult(RESULT_OK);
            finish();
            Toast.makeText(myActivity, "保存成功", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(myActivity, "该标题已存在", Toast.LENGTH_LONG).show();
        }

    }
}
